package com.example.HaiDLaobackend.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.HaiDLaobackend.common.ApiResponse;
import com.example.HaiDLaobackend.entity.OrderEntity;
import com.example.HaiDLaobackend.entity.ProductReviewEntity;
import com.example.HaiDLaobackend.service.OrderService;
import com.example.HaiDLaobackend.service.ProductReviewService;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class ReviewController {

    private final ProductReviewService productReviewService;
    private final OrderService orderService;

    public ReviewController(ProductReviewService productReviewService, OrderService orderService) {
        this.productReviewService = productReviewService;
        this.orderService = orderService;
    }

    @GetMapping("/api/products/{productId}/reviews")
    public ApiResponse<List<ProductReviewEntity>> listByProduct(@PathVariable Long productId,
                                                                @RequestParam(defaultValue = "1") long pageNum,
                                                                @RequestParam(defaultValue = "10") long pageSize) {
        Page<ProductReviewEntity> page = productReviewService.page(new Page<>(pageNum, pageSize),
                new LambdaQueryWrapper<ProductReviewEntity>()
                        .eq(ProductReviewEntity::getProductId, productId)
                        .orderByDesc(ProductReviewEntity::getId));
        return ApiResponse.ok(page.getRecords());
    }

    public static class CreateBody {
        public Long productId;
        public Long orderId;
        public Long orderItemId;
        public Integer rating;
        public String content;
        public String images;
    }

    @PostMapping("/api/reviews")
    public ApiResponse<ProductReviewEntity> create(@RequestAttribute(value = "currentUserId", required = false) Long currentUserId,
                                                   @RequestBody CreateBody body) {
        if (currentUserId == null) return ApiResponse.error(401, "未登录");
        if (body == null || body.productId == null || body.orderId == null || body.rating == null) {
            return ApiResponse.error(400, "参数不完整");
        }
        LambdaQueryWrapper<ProductReviewEntity> wrapper = new LambdaQueryWrapper<ProductReviewEntity>()
                .eq(ProductReviewEntity::getUserId, currentUserId)
                .eq(ProductReviewEntity::getOrderId, body.orderId);
        if (body.orderItemId != null) wrapper.eq(ProductReviewEntity::getOrderItemId, body.orderItemId);
        wrapper.last("LIMIT 1");
        ProductReviewEntity exists = productReviewService.getOne(wrapper);
        if (exists != null) {
            return ApiResponse.error(400, "您已评价过该商品");
        }
        ProductReviewEntity r = new ProductReviewEntity();
        r.setProductId(body.productId);
        r.setOrderId(body.orderId);
        r.setOrderItemId(body.orderItemId);
        r.setUserId(currentUserId);
        r.setRating(body.rating);
        r.setContent(body.content);
        r.setImages(body.images);
        r.setStatus(1);
        productReviewService.save(r);
        orderService.update(new LambdaUpdateWrapper<OrderEntity>()
                .eq(OrderEntity::getId, body.orderId)
                .eq(OrderEntity::getUserId, currentUserId)
                .set(OrderEntity::getStatus, "REVIEWED"));
        return ApiResponse.ok(r);
    }
}


